
BDS456C MERN Laboratory
Dept, Of CSE (Data Science), BIT 2023-2024 Page 53
PROGRAM 8
8. Develop two routes: find_prime_100 and find_cube_100 which prints prime
numbers less than 100 and cubes less than 100 using Express JS routing mechanism
DESCRIPTION
The routing mechanism in Express.js defines how your application responds to client
requests based on the URL and HTTP method (GET, POST, PUT, DELETE, etc.).
Express.js provides a simple and flexible routing system that allows you to define routes for
different endpoints of your application.
Here's how the routing mechanism works in Express.js:
1. Defining Routes: You define routes using the app.METHOD() functions, where
METHOD is the HTTP method of the request (e.g., GET, POST, PUT, DELETE). These
functions take two arguments: the URL pattern (or route path) and a callback function that
specifies what should happen when a request matches that route.
app.METHOD(path, callback);
2. Matching Requests: When a client makes a request to your server, Express.js matches
the requested URL and HTTP method against the defined routes. If there's a match,
Express.js executes the corresponding callback function.
3. Executing Middleware: In Express.js, you can attach one or more middleware functions
to a route. Middleware functions are functions that have access to the request object (req),
the response object (res), and the next middleware function in the application's request-
response cycle. Middleware functions can perform tasks such as data validation,
authentication, logging, etc.
4. Handling Responses: Inside the route's callback function, you typically send a response
back to the client using methods of the res object (e.g., res.send(), res.json(), res.render()).
This response can be in various formats, such as HTML, JSON, or others, depending on the
client's request and the application's requirements.